package com.quan.upms.web.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.upms.rpc.api.UpmsGroupService;
import com.quan.upms.rpc.model.UpmsGroup;

@RestController
@RequestMapping(value = "/api/upms/group")
public class UpmsGroupController extends BaseController {

	@Autowired
	private UpmsGroupService upmsGroupService;

	@RequiresPermissions(value="upms:group:add", description="添加群组")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody UpmsGroup entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if (null != this.upmsGroupService.checkValue("name", entity.getName())) {
			return BaseResult.failure("名称已存在");
		}
		if (null != this.upmsGroupService.checkValue("code", entity.getCode())) {
			return BaseResult.failure("编码已存在");
		}
		this.upmsGroupService.insertSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:group:delete", description="删除群组")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsGroupService.delete(id);
		return BaseResult.ok();
	}
	
	@RequiresPermissions(value="upms:group:delete", description="删除群组")
	@GetMapping(value = "/deleteBatch")
	public Object deleteBatch(String ids) {
		this.upmsGroupService.deleteByIds(ids) ;
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:group:update", description="修改群组")
	@PostMapping(value = "/update")
	public Object update(@RequestBody UpmsGroup entity) {
		if (StringUtils.isBlank(entity.getName())) {
			return BaseResult.failure("名称号码不能为空");
		}
		if (StringUtils.isBlank(entity.getCode())) {
			return BaseResult.failure("编码不能为空");
		}
		UpmsGroup oldGroup = this.upmsGroupService.selectById(entity.getId()) ;
		if(null != oldGroup.getName() && !oldGroup.getName().equals(entity.getName())) {
			if(null != this.upmsGroupService.checkValue("name", entity.getName())) {
				return BaseResult.failure("名称已存在");
			}
		}
		if(null != oldGroup.getCode() && !oldGroup.getCode().equals(entity.getCode())) {
			if(null != this.upmsGroupService.checkValue("code", entity.getCode())) {
				return BaseResult.failure("编码已存在");
			}
		}
		this.upmsGroupService.updateSelective(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:group:detail", description="查询群组详情")
	@GetMapping(value = "/get/{id}")
	public Object get(@PathVariable Integer id) {
		return this.upmsGroupService.selectById(id);
	}

	@RequiresPermissions(value="upms:group:list", description="查询群组")
	@GetMapping(value = "/list")
	public Object list(UpmsGroup entity) {
		return this.upmsGroupService.selectPage(new Paginator(this.request), entity);
	}
	
	/**
	 * 将用户移出组
	 * @param groupAssingId
	 * @return
	 */
	@RequiresPermissions(value="upms:group:remove_user_assign", description="移除该群组成员")
	@GetMapping(value = "/remove_group_users")
	public Object remove_group_users(String ids) {
		this.upmsGroupService.removeGroupUsers(ids) ;
		return BaseResult.ok();
	}
}
